package com.cxm.weilai.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * 实例化包.
 *
 * @author admin
 */
public class SerialUtil {
    /**
     * 编码
     *
     * @param object 传入参数
     * @return 字节数组
     */
    public static byte[] encode(Object object) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = null;
        byte[] data = null;
        try {
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            data = baos.toByteArray();
        } catch (IOException ioException) {
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
            } catch (IOException ioException2) {
            }
        }
        return data;
    }

    /**
     * 编码 将字节数组转Object
     */
    public static Object decode(byte[] data) {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        ObjectInputStream ois = null;
        Object object = null;
        try {
            ois = new ObjectInputStream(bais);
            object = ois.readObject();
        } catch (IOException ioException) {
        } catch (ClassNotFoundException classNotFoundException) {
        } finally {
            try {
                if (ois != null) {
                    ois.close();
                }
            } catch (IOException ioException) {
            }
        }
        return object;
    }
}